package edu.nf.waimai.dao.admin.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.admin.AdminUserDao;
import edu.nf.waimai.model.user.User;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-09-12
 * @Author qiu
 */
public class UserDaoImpl implements AdminUserDao {


    @Override
    public List<User> getAllUser() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select u_id,u_name,u_pwd,u_nickname,u_phone,u_avatar,u_datetime from users";

        BeanListHandler<User> userBeanHandler = new BeanListHandler<>(User.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler);
    }

    @Override
    public int updateUser(User user) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update users set u_name = ?,u_pwd=?,u_nickname=?,u_phone=?,u_avatar=? where u_id = ?";
        return sqlExecutor.executeUpdate(sql,user.getUid(),user.getUname(),user.getUpwd(),user.getUphone(),user.getUavatar());
    }

    @Override
    public int deleteUser(int uid) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from users where u_id = ?";
        return sqlExecutor.executeUpdate(sql,uid);
    }

    @Override
    public int addUser(User user) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql ="insert into users(u_name,u_pwd,u_nickname,u_phone,u_avatar) values(?,?,?,?,?)";
        return sqlExecutor.executeUpdate(sql,user.getUname(),user.getUpwd(),user.getUnickname(),user.getUphone(),user.getUavatar());
    }

    @Override
    public List<User> getUser() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select order_id,u_name,shop_name,province,city,district,detail,rider_name,total_price,order_type,order_datetime,order_remarks from orders \n" +
                " left join users on users.u_id = orders.u_id\n" +
                " left join shop on shop.shop_id = orders.shop_id\n" +
                " left join address on address.address_id = orders.address_id\n" +
                " left join rider on rider.rider_id = orders.rider_id \n" +
                " where order_status=6;";
        BeanListHandler<User> userHandler = new BeanListHandler<>(User.class);
        return sqlExecutor.executeQuery(sql,userHandler);
    }

}
